package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AdditionalVariableAddDTO;
import org.jsola.hr.dto.AdditionalVariableBatchDTO;
import org.jsola.hr.dto.AdditionalVariableUpdateDTO;
import org.jsola.hr.query.AdditionalVariableQuery;
import org.jsola.hr.service.IAdditionalVariableService;
import org.jsola.hr.vo.AdditionalVariableListVO;
import org.jsola.hr.vo.AdditionalVariableVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 员工自定义信息关联变量
 *
 * @author zhr
 */
@RestController("hrAdditionalVariableController")
@RequestMapping(HrConstants.API_PATH + "/v1/additionalVariables")
public class AdditionalVariableController implements IAdditionalVariableControllerAPI {

    @Autowired
    private IAdditionalVariableService additionalVariableService;

    @Override
    @PostMapping
    public Result<AdditionalVariableVO> save(@Valid @RequestBody AdditionalVariableAddDTO additionalVariableAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工自定义信息关联变量权限校验

        return Result.success(
                additionalVariableService.save(additionalVariableAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AdditionalVariableUpdateDTO additionalVariableUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工自定义信息关联变量权限校验

        int count = additionalVariableService.update(additionalVariableUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody AdditionalVariableBatchDTO additionalVariableBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<AdditionalVariableAddDTO> additionalVariableAddDTOList = additionalVariableBatchDTO.getAdditionalVariableAddDTOList();
        if (!CollectionUtils.isEmpty(additionalVariableAddDTOList)) {
            additionalVariableService.batchSave(additionalVariableAddDTOList,tokenUser);
        }
        // 修改
        List<AdditionalVariableUpdateDTO> additionalVariableUpdateDTOList = additionalVariableBatchDTO.getAdditionalVariableUpdateDTOList();
        if (!CollectionUtils.isEmpty(additionalVariableUpdateDTOList)) {
            additionalVariableUpdateDTOList.parallelStream().forEach(
                    additionalVariableUpdateDTO -> {
                        additionalVariableService.update(additionalVariableUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<Long> deleteAdditionalVariableIdList = additionalVariableBatchDTO.getDeleteAdditionalVariableIdList();
        if (!CollectionUtils.isEmpty(deleteAdditionalVariableIdList)) {
            additionalVariableService.deleteByIds(tokenUser,deleteAdditionalVariableIdList.toArray(new Long[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{additionalVariableId}")
    public Result<Integer> delete(@PathVariable Long additionalVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工自定义信息关联变量权限校验

        int count = additionalVariableService.deleteByIds(tokenUser, additionalVariableId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] additionalVariableIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工自定义信息关联变量权限校验

        int count = additionalVariableService.deleteByIds(tokenUser, additionalVariableIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{additionalVariableId}")
    public Result<AdditionalVariableVO> findById(@PathVariable Long additionalVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工自定义信息关联变量权限校验

        return Result.success(
            additionalVariableService.selectById(additionalVariableId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AdditionalVariableListVO>> listAll(@Valid AdditionalVariableQuery additionalVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工自定义信息关联变量权限校验

        return Result.success(
            additionalVariableService.select(additionalVariableQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AdditionalVariableListVO>> page(@Valid AdditionalVariableQuery additionalVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            additionalVariableService.selectPage(additionalVariableQuery, tokenUser.getSiteId())
        );
    }


}

